package com.haoxi.gateway.cngpmsg;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.haoxi.gateway.util.TypeConvert;

public class CNGPDeliverMessage extends CNGPMessage {
    protected int deliverType;

    public CNGPDeliverMessage(byte buf[]) throws IllegalArgumentException {
        int len = 90 + (buf[84] & 0xff);
        if (buf.length != len) {
            throw new IllegalArgumentException(
                    "The message that is sent by SMC is wrong");
        } else {
            super.buf = new byte[len];
            System.arraycopy(buf, 0, super.buf, 0, buf.length);
            deliverType = buf[26];
        }
    }

    public byte[] getMsgId() {
        byte msgId[] = new byte[10];
        System.arraycopy(buf, 16, msgId, 0, 10);
        return msgId;
    }

    public int getIsReport() {
        return buf[26];
    }

    public int getMsgFormat() {
        return buf[27];
    }

    public Date getRecvTime() {
        Date date;
        try {
            int tmpYear = TypeConvert.byte2int(buf, 27);
            byte tmpbyte[] = new byte[2];
            System.arraycopy(buf, 31, tmpbyte, 0, 2);
            String tmpstr = new String(tmpbyte);
            int tmpMonth = Integer.parseInt(tmpstr);
            System.arraycopy(buf, 33, tmpbyte, 0, 2);
            tmpstr = new String(tmpbyte);
            int tmpDay = Integer.parseInt(tmpstr);
            System.arraycopy(buf, 35, tmpbyte, 0, 2);
            tmpstr = new String(tmpbyte);
            int tmpHour = Integer.parseInt(tmpstr);
            System.arraycopy(buf, 37, tmpbyte, 0, 2);
            tmpstr = new String(tmpbyte);
            int tmpMinute = Integer.parseInt(tmpstr);
            System.arraycopy(buf, 39, tmpbyte, 0, 2);
            tmpstr = new String(tmpbyte);
            int tmpSecond = Integer.parseInt(tmpstr);
            Calendar calendar = Calendar.getInstance();
            calendar.set(tmpYear, tmpMonth, tmpDay, tmpHour, tmpMinute,
                    tmpSecond);
            date = calendar.getTime();
        } catch (Exception e) {
            date = null;
        }
        return date;
    }

    public String getSrcTermID() {
        byte srcTermId[] = new byte[21];
        System.arraycopy(buf, 41, srcTermId, 0, 21);
        return (new String(srcTermId)).trim();
    }

    public String getDestTermID() {
        byte destTermId[] = new byte[21];
        System.arraycopy(buf, 62, destTermId, 0, 21);
        return (new String(destTermId)).trim();
    }

    public int getMsgLength() {
        return buf[84] & 0xff;
    }

    public byte[] getMsgContent() {
        int len = buf[84] & 0xff;
        byte content[] = new byte[len];
        System.arraycopy(buf, 85, content, 0, len);
        return content;
    }

    public int getCongestionState() {
        int pos = 89 + (buf[84] & 0xff);
        return buf[pos];
    }

    // 处理状态报告,这里的ID应该就是submitrep中的msgid,实际是否这样还要进行测试
    public byte[] getID() {
        if (1 == deliverType) {
            byte idbytes[] = new byte[7];
            System.arraycopy(buf, 85, idbytes, 0, 10);
            return idbytes;
        } else {
            return null;
        }
    }

    public String getSub() {
        if (1 == deliverType) {
            byte[] subbytes = new byte[3];
            System.arraycopy(buf, 95, subbytes, 0, 3);
            return (new String(subbytes)).trim();
        } else {
            return null;
        }
    }

    public String getDlvrd() {
        if (1 == deliverType) {
            byte[] dlvrdbytes = new byte[3];
            System.arraycopy(buf, 98, dlvrdbytes, 0, 3);
            return (new String(dlvrdbytes)).trim();
        } else {
            return null;
        }
    }

    public Date getSubmitTime() {
        if (deliverType == 1) {
            byte tmpbyte[] = new byte[2];
            System.arraycopy(buf, 101, tmpbyte, 0, 2);
            String tmpstr = new String(tmpbyte);
            int tmpYear = 2000 + Integer.parseInt(tmpstr);
            System.arraycopy(buf, 103, tmpbyte, 0, 2);
            tmpstr = new String(tmpbyte);
            int tmpMonth = Integer.parseInt(tmpstr) - 1;
            System.arraycopy(buf, 105, tmpbyte, 0, 2);
            tmpstr = new String(tmpbyte);
            int tmpDay = Integer.parseInt(tmpstr);
            System.arraycopy(buf, 107, tmpbyte, 0, 2);
            tmpstr = new String(tmpbyte);
            int tmpHour = Integer.parseInt(tmpstr);
            System.arraycopy(buf, 109, tmpbyte, 0, 2);
            tmpstr = new String(tmpbyte);
            int tmpMinute = Integer.parseInt(tmpstr);
            Calendar calendar = Calendar.getInstance();
            calendar.set(tmpYear, tmpMonth, tmpDay, tmpHour, tmpMinute);
            return calendar.getTime();
        } else {
            return null;
        }
    }

    public Date getDoneTime() {
        if (deliverType == 1) {
            byte tmpbyte[] = new byte[2];
            System.arraycopy(buf, 111, tmpbyte, 0, 2);
            String tmpstr = new String(tmpbyte);
            int tmpYear = 2000 + Integer.parseInt(tmpstr);
            System.arraycopy(buf, 113, tmpbyte, 0, 2);
            tmpstr = new String(tmpbyte);
            int tmpMonth = Integer.parseInt(tmpstr) - 1;
            System.arraycopy(buf, 115, tmpbyte, 0, 2);
            tmpstr = new String(tmpbyte);
            int tmpDay = Integer.parseInt(tmpstr);
            System.arraycopy(buf, 117, tmpbyte, 0, 2);
            tmpstr = new String(tmpbyte);
            int tmpHour = Integer.parseInt(tmpstr);
            System.arraycopy(buf, 119, tmpbyte, 0, 2);
            tmpstr = new String(tmpbyte);
            int tmpMinute = Integer.parseInt(tmpstr);
            Calendar calendar = Calendar.getInstance();
            calendar.set(tmpYear, tmpMonth, tmpDay, tmpHour, tmpMinute);
            return calendar.getTime();
        } else {
            return null;
        }
    }

    public String getStat() {
        if (deliverType == 1) {
            byte tmpStat[] = new byte[7];
            System.arraycopy(buf, 121, tmpStat, 0, 7);
            return (new String(tmpStat)).trim();
        } else {
            return null;
        }
    }

    public String getErr() {
        if (deliverType == 1) {
            byte tmpErr[] = new byte[3];
            System.arraycopy(buf, 128, tmpErr, 0, 3);
            return (new String(tmpErr)).trim();
        } else {
            return null;
        }
    }

    public String getSubmitMsgLenth() {
        if (deliverType == 1) {
            byte tmpLength[] = new byte[3];
            System.arraycopy(buf, 131, tmpLength, 0, 3);
            return (new String(tmpLength)).trim();
        } else {
            return null;
        }
    }

    public String getSubmitContent() {
        if (deliverType == 1) {
            byte tmpLength[] = new byte[17];
            System.arraycopy(buf, 134, tmpLength, 0, 17);
            return (new String(tmpLength)).trim();
        } else {
            return null;
        }
    }

    public String toString() {
        StringBuffer strBuf = new StringBuffer();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        strBuf.append("CNGPDeliverMessage: ");
        strBuf.append("PacketLength=").append(getMsgLength());
        strBuf.append(",RequestID=").append(getRequestId());
        strBuf.append(",Status=").append(getStatus());
        strBuf.append(",Sequence_Id=").append(getSequenceId());
        strBuf.append(",MsgID=").append(getMsgId());
        strBuf.append(",IsReport=").append(getIsReport());
        strBuf.append(",MsgFormat=").append(getMsgFormat());
        if (getRecvTime() != null) {
            strBuf.append(",RecvTime=")
                    .append(dateFormat.format(getRecvTime()));
        } else {
            strBuf.append(",RecvTime=null");
        }
        strBuf.append(",SrcTermID=").append(getSrcTermID());
        strBuf.append(",DestTermID=").append(getDestTermID());
        strBuf.append(",MsgLength=").append(getMsgLength());
        if (0 == deliverType) {
            strBuf.append(",MsgContent=").append(new String(getMsgContent()));
        } else if (1 == deliverType) {
            dateFormat = new SimpleDateFormat("yyMMddHHmm");
            strBuf.append(",SubmitMsgId=").append(getID());
            strBuf.append(",Sub=").append(getSub());
            strBuf.append(",Dlvrd=").append(getDlvrd());
            strBuf.append(",SubmitTime=").append(
                    dateFormat.format(getSubmitTime()));
            strBuf.append(",DoneTime=")
                    .append(dateFormat.format(getDoneTime()));
            strBuf.append(",Stat=").append(getStat());
            strBuf.append(",Err=").append(getErr());
            strBuf.append(",Submitmsglength=").append(getSubmitMsgLenth());
            strBuf.append(",SubmitContent=").append(getSubmitContent());

        }

        strBuf.append(",CongestionState=").append(getCongestionState());
        return strBuf.toString();
    }
}
